<template>
  <div class="home">
    <!-- 顶部导航栏 -->
    <Header />
    
    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 轮播图 -->
      <section class="banner-section">
        <a-carousel autoplay>
          <div v-for="banner in banners" :key="banner.id">
            <img :src="banner.image" :alt="banner.title" class="banner-image" />
          </div>
        </a-carousel>
      </section>
      
      <!-- 商品分类 -->
      <section class="category-section">
        <div class="container">
          <h2 class="section-title">{{ $t('nav.categories') }}</h2>
          <div class="category-grid">
            <div 
              v-for="category in categories" 
              :key="category.id"
              class="category-item"
              @click="goToCategory(category.name)"
            >
              <div class="category-icon">{{ category.icon }}</div>
              <div class="category-name">{{ category.name }}</div>
            </div>
          </div>
        </div>
      </section>
      
      <!-- 热门商品 -->
      <section class="hot-products-section">
        <div class="container">
          <h2 class="section-title">{{ $t('home.featuredProducts') }}</h2>
          <div class="products-grid">
            <ProductCard 
              v-for="product in hotProducts" 
              :key="product.id"
              :product="product"
              @add-to-cart="addToCart"
            />
          </div>
        </div>
      </section>
      
      <!-- 推荐商品 -->
      <section class="recommend-section">
        <div class="container">
          <h2 class="section-title">{{ $t('home.specialOffers') }}</h2>
          <div class="products-grid">
            <ProductCard 
              v-for="product in recommendProducts" 
              :key="product.id"
              :product="product"
              @add-to-cart="addToCart"
            />
          </div>
        </div>
      </section>
      
      <!-- 公告信息 -->
      <section class="announcement-section" v-if="announcements.length > 0">
        <div class="container">
          <h2 class="section-title">{{ $t('home.announcements') || '最新公告' }}</h2>
          <div class="announcement-list">
            <div 
              v-for="announcement in announcements" 
              :key="announcement.id"
              class="announcement-item"
            >
              <span class="announcement-title">{{ announcement.title }}</span>
              <span class="announcement-date">{{ formatDate(announcement.date) }}</span>
            </div>
          </div>
        </div>
      </section>
    </div>
    
    <!-- 底部信息 -->
    <Footer />
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import ProductCard from '@/components/ProductCard.vue'
import { systemApi } from '@/api'

// 使用Vuex store
const store = useStore()
const router = useRouter()

// 响应式数据
const banners = ref([])
const announcements = ref([])

// 计算属性
const categories = computed(() => store.getters['product/categories'])
const hotProducts = computed(() => store.getters['product/products'])
const recommendProducts = computed(() => store.getters['product/products'])

// 方法
const goToCategory = (categoryName) => {
  router.push(`/category/${categoryName}`)
}

const addToCart = (product) => {
  store.dispatch('cart/addToCart', product)
  message.success('已添加到购物车')
}

const formatDate = (dateString) => {
  return new Date(dateString).toLocaleDateString('zh-CN')
}

// 生命周期
onMounted(async () => {
  try {
    // 加载轮播图数据
    const bannersResponse = await systemApi.getBanners()
    if (bannersResponse.code === 200) {
      banners.value = bannersResponse.data
    }
    
    // 加载公告数据
    const announcementsResponse = await systemApi.getAnnouncements()
    if (announcementsResponse.code === 200) {
      announcements.value = announcementsResponse.data
    }
    
    // 加载商品分类
    await store.dispatch('product/fetchCategories')
    
    // 加载热门商品
    await store.dispatch('product/fetchProducts', { page: 1 })
    
    // 加载推荐商品
    await store.dispatch('product/fetchProducts', { page: 2 })
  } catch (error) {
    console.error('加载首页数据失败:', error)
    message.error('加载数据失败，请刷新页面重试')
  }
})
</script>

<style scoped>
.home {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.main-content {
  flex: 1;
}

/* 轮播图样式 */
.banner-section {
  min-height: 400px;
  margin-bottom: 30px;
}

.banner-image {
  width: 100%;
  height: 400px;
  object-fit: cover;
}

/* 分类样式 */
.category-section {
  padding: 40px 0;
  background-color: white;
  margin-bottom: 30px;
}

.section-title {
  text-align: center;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #333;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  border-radius: 8px;
  background-color: #f8f9fa;
  cursor: pointer;
  transition: all 0.3s ease;
}

.category-item:hover {
  background-color: #e9ecef;
  transform: translateY(-2px);
}

.category-icon {
  font-size: 40px;
  margin-bottom: 10px;
}

.category-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

/* 商品网格样式 */
.hot-products-section,
.recommend-section {
  padding: 40px 0;
  background-color: white;
  margin-bottom: 30px;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

/* 公告样式 */
.announcement-section {
  padding: 40px 0;
  background-color: white;
  margin-bottom: 30px;
}

.announcement-list {
  max-width: 600px;
  margin: 0 auto;
}

.announcement-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.announcement-item:last-child {
  border-bottom: none;
}

.announcement-title {
  font-size: 16px;
  color: #333;
}

.announcement-date {
  font-size: 14px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .banner-image {
    height: 200px;
  }
  
  .category-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
  }
  
  .products-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 15px;
  }
  
  .section-title {
    font-size: 24px;
  }
  
  .announcement-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
}
</style> 